let that;

let paramWork;
terminalwork = new Vue({
	el: "#terminalwork",
	data: {
		isShowList: false,
		currentUser: '', // 用户信息

		terminalKeyword: '', // 关键字
		currentPage: 1, // 当前页
		terminalworkList: [], // 定期工作列表
		// 码表
		checkResultTypeList:[],
		troubleWorkTypeList:[],
		troubleStatusList:[],
		Record:[],
		typeList:[],
		popicker:"",
		workTypeTxt:"",
		searchType:"",//当前获取的workType
		
	},
	components: {
		personselect,
		multipleselect
	},
	created() {
		that = this;
		setTimeout(that.downCode(), 500);
		setTimeout(that.initList(), 500);
	},
	mounted: function() {
		// 获取用户信息
		this.currentUser = JSON.parse(localStorage.getItem('neiosUser'))
		// 获取码表 
		let dictCodes = 'YES_OR_NO,TROUBLE_WORK_TYPE,TROUBLE_STATUS';
		let _this = this
		let dict = mui.neiosCommon.queryDict(dictCodes, function(data) {
			if (data != '') {
				_this.checkResultTypeList = data.YES_OR_NO
				_this.troubleWorkTypeList = data.TROUBLE_WORK_TYPE
				_this.troubleStatusList = data.TROUBLE_STATUS
			} 
			// 获取列表 
			// _this.queryTerminalList(_this.currentPage);
		})
	},
	methods: {
		//获取初始化数据
		initList(){
			let typeListArray = mui.neioConfig.workType();
			that.workTypeTxt = typeListArray[0].text;
			that.searchType = typeListArray[0].value;
			
			let param = {
				current: that.currentPage,
				pageSize: 10,
				workType:typeListArray[0].value,
				areaCompanyCode:"",
				stationCode:localStorage.getItem('neiosStationCode'),
				role:'NEIOS_STATION',
				searchStr:that.terminalKeyword
			}
			paramWork = typeListArray[0];
			this.getWorListByType(typeListArray[0],that.currentPage);
		},
		//获取业务类型列表
		getWorkTypeList(){
			mui.openWindow({
				url: "workTypeList.html",
				id: "workTypeList.html",
				extras: {},
				waiting: {
					autoShow: false
				}
			});
		},
		// 码表转换
		dictChange (list,data) {
			return mui.neiosCommon.dictTransform(list,data)
		},
		//显示窗口 [{value:'zz',text:'智子'}]
		showPiker(){
			that.popicker.setData(that.typeList);
			that.popicker.show(function (selectItems) {
			   console.log(selectItems[0].text);//智子
			   console.log(selectItems[0].value);//zz 
			 })
		},
		// 查询列表
		queryTerminalList(current) {
			let url = '/app/personWork/getStatisticalList'
			let _this = this
			let param = {
				current: current,
				pageSize: 10,
				keyWord:this.terminalKeyword,
				// companyCode:this.currentUser.companyNewCode,
				companyCode:'', 
				stationCode:"",
				// stationCode:localStorage.getItem('neiosStationCode'),
			}
			console.log(">>>>>>:"+JSON.stringify(param))
			mui.neiosRequst.httpRequst(url, param,
				function(data) {
					if (data.code == '0') {
						_this.isShowList = true
						console.log(JSON.stringify(data.data.records[0]))
						if (_this.currentPage == 1) {
							mui('#terminalworkList').pullRefresh().enablePullupToRefresh();
							mui('#terminalworkList').pullRefresh().endPullupToRefresh();
							_this.terminalworkList = data.data.records
						} else {
							mui('#terminalworkList').pullRefresh().endPullupToRefresh();
							_this.terminalworkList = _this.terminalworkList.concat(data.data.records)
						}
						if (data.data.current >= data.data.pages) {
							console.log("====================禁用====================")  
							mui('#terminalworkList').pullRefresh().disablePullupToRefresh();
							mui('#terminalworkList').pullRefresh().endPullupToRefresh(true);
						}
					} else {
						_this.terminalworkList = []
						mui.toast('查询列表失败，请稍后重试！')
					}
				},
				function() {
					_this.terminalworkList = [] 
					mui.toast('查询列表失败，请稍后重试！')
				}
			);
		},
		// 关键字搜索
		terminalKeywordChange() {
			this.currentPage = 1
			this.getWorListByType(paramWork,1)
			this.isShowList = false
		},
		
		// 跳转详情
		goDetailPage (row) {
			console.log("选择数据:"+JSON.stringify(row))
			let url = ''
			if (row.workType == '0101') { // 蓄电池
				url = '../trouble/details/storBatteryDet.html'
			} else if (row.workType == '0102' || row.workType == '0103'
			 || row.workType == '0104' || row.workType == '0105'
			 || row.workType == '0106' || row.workType == '0107'
			 || row.workType == '0108') { // 场用电、UPS定期检查、事故照明定期检查、主变风冷定期检查、直流切换定期检查、消防切换定期检查、通信定期检查
				url = '../trouble/details/powerSystemDet.html'
			} else if (row.workType == '0109') { // 柴油机
				url = '../trouble/details/dieselGerDet.html'
			} else if (row.workType == '0110' || row.workType == '0207') { // 工器具
				url = '../trouble/details/toolCheckDet.html'
			} else if (row.workType == '0111') { // 安全设施定期
				url = '../trouble/details/safeDeviceDet.html' 
			} else if (row.workType == '0113' || that.searchType == '0113') { // 生活消防水设备定期检查
				url = '../trouble/details/lifeWaterDet.html' 
			} else if (row.workType == '0114' || that.searchType == '0114') { // 设备红外定期检查
				url = '../trouble/details/infraredDet.html' 
			} else if (row.workType == '0115') { // 网络安全防护设备定期检查
				url = '../trouble/details/netSafeDeviceDet.html' 
			} else if (row.workType == '0117') { // 避雷器动作记录
				url = '../trouble/details/lightningArresterDet.html' 
			} else if (row.workType == '0201' || that.searchType == '0201') { // 交接班
				url = '../dailyRecord/shiftsChange/handOver.html';
				localStorage.setItem("workId", row.workId);
				mui.openWindow({
					url: url,
					id: url,
					extras: {},
					waiting: {
						autoShow: false
					}
				});
				return
			} else if (row.workType == '0203' || that.searchType == '0202,0203') { // 钥匙归还借用
					let param = {
						obj: row,
						alwinType: 0
					}
					console.log("参数2："+JSON.stringify(param))
					localStorage.setItem('neiosGoDetail',JSON.stringify(param))
					
					if(row.inWorkId != null && row.outWorkId == null){
						//归还钥匙
						url = '../dailyRecord/keyBorrow/detail.html';
						mui.openWindow({
							url: url,
							id:  url,
							extras: {
								neiosGoDetail:JSON.stringify(row)
							},
							waiting: {
								autoShow: false
							}
						});
						return;
					}
					
					if(row.inWorkId == null && row.outWorkId != null){
						//借用钥匙
						url = '../dailyRecord/keyBorrow/returnDetail.html';
						mui.openWindow({
							url: url,
							id:  url,
							extras: {
								neiosGoDetail:JSON.stringify(row)
							},
							waiting: {
							autoShow: false
							}
						});
						return;
					}
		
					plus.nativeUI.actionSheet(
						{title:"请选择",
						cancel:"取消",
						buttons:[
							{
								title:"钥匙借用"
							},{
								title:"钥匙归还"
								}
						]},
						function(e){
							if(e.index == '1'){
								url = '../dailyRecord/keyBorrow/detail.html';
								mui.openWindow({
									url: url,
									id:  url,
									extras: {
										neiosGoDetail:JSON.stringify(row)
									},
									waiting: {
										autoShow: false
									}
								});
							}
							if(e.index == '2'){
								url = '../dailyRecord/keyBorrow/returnDetail.html';
								mui.openWindow({
									url: url,
									id:  url,
									extras: {
										neiosGoDetail:JSON.stringify(row)
									},
									waiting: {
									autoShow: false
									}
								});
							}
						}
					);
				return
			} else if (row.workType == '0209' || row.workType == '0204' || that.searchType == '0204,0209') { // 涉网设备退出记录
				url = '../dailyRecord/network/detail.html'
				let config = mui.neioConfig.getNeioConfig();
				let key = config.KEYS.NET_WORK_DETAIL;
				mui.neioConfig.setStorage(key,JSON.stringify(row));
			} else if (row.workType == '0205' || row.workType == '0208' || that.searchType == '0205,0208') { // 保护压板投退记录
				
				url = '../trouble/details/detailDet.html'
				let config = mui.neioConfig.getNeioConfig();
				let key = config.KEYS.PLATENDATA;
				mui.neioConfig.setStorage(key,JSON.stringify(row));
			} else if (row.workType == '0111' || row.workType == '0111') { // 安全设施
				url = ''
			} else if (row.workType == '0112' || that.searchType == '0112') { // 差动电流
				url = '../trouble/details/diffCurrentDet.html';
			} else if (row.workType == '0206' || that.searchType == '0206') { // 变压器有载调压
				let param = {};
				let workId = row.workId;
				let transOperateId = row.transOperateId;
				param.workId = workId;
				param.transOperateId = transOperateId;
				param.pageType = '1'; //0 录入|新增 1 展示/详情 2编辑修改 3办理,5办理离线
				param.listType = '90';
				param.operateStaffName = row.operateStaffName;
				param.operateStaffCode = row.operateStaffCode;
				param.tutelageStaffCode = row.tutelageStaffCode;
				param.tutelageStaffName = row.tutelageStaffName;
				let config = mui.neioConfig.getNeioConfig();
				let pageKey = config.KEYS.PAGE_PARAM;
				mui.neioConfig.setStorage(pageKey,JSON.stringify(param))
				console.log("详情参数:"+JSON.stringify(param))
				url = "../dailyRecord/transformer/procressing.html"

			} else if (row.workType == '0116') { // 主变铁芯、夹地
				url = '../trouble/details/mainTransCoreDet.html'
			}
			// else if (row.workType == '0301' || that.searchType == '01') { // 教育培训 安全活动
			// 	url = '../trouble/details/trainEditDet.html'
			// } else if (row.workType == '0302' || that.searchType == '00') { // 应急演练
			// 	url = '../trouble/details/drillEditDet.html'
			// }
			else if(['00','01','10','11','12'].includes(that.searchType)){
				let url = '../education/drill.html';
				if(row.activityType >=10){
					url="../education/drill.html"
				}
				else{
					url="../education/train.html"
				}
				localStorage.setItem('trainingDrill', JSON.stringify(row));
				mui.openWindow({
					url,
					id: url
				});
				return;
			}

			let param = {
				obj: row,
				alwinType: 0
			}
			localStorage.setItem("neiosGoDetail", JSON.stringify(param))
			mui.openWindow({
				url: url,
				id: url,
				extras: {},
				waiting: {
					autoShow: false
				}
			});
		},
		//获取变压器有载调压数据
		//根据workid 获取
		getDataFromWorkId(workId){
			let config = mui.neioConfig.getNeioConfig();
			let url = config.URL.queryTransInfoByWorkId;
			let params = {};
			params.workId = workId;
			console.log("变压器："+JSON.stringify(params))
			mui.neiosRequst.httpRequst(url,params,function(ret){
				console.log(JSON.stringify(ret))
				if(ret.code == 0){
					console.log(ret)
					//列表赋值
					that.Record = ret;

					url = "../dailyRecord/transformer/detail.html";
					
					let param = that.Record;
							
					param.position = 0;//当前选择的第几项
					param.mType = "1";  //0 录入|新增 1 展示/详情 2编辑修改 3办理
					param.isSearch = 1;
					let params = JSON.stringify(param);
					console.log("准备发射===>:"+JSON.stringify(params))
					let config = mui.neioConfig.getNeioConfig();
					let pageKey = config.KEYS.PAGE_PARAM_TRANSFORM_INFO;
					let paramStr = mui.neioConfig.setStorage(pageKey,params);
					
					mui.openWindow({
						url: url,
						id: url,
						extras: {},
						waiting: {
							autoShow: false
						}
					});
					console.log("KLKLK"+JSON.stringify(dataParam))
				}
				if( ret.code == 1){
						mui.toast(ret.msg)
				}
			},function(err){
				console.log("失败回掉："+JSON.stringify(err))
			})
		},
		//初始化变压器类型
		//加载码表数据
		downCode(){
			//获取Config
			let config = mui.neioConfig.getNeioConfig();
			//清空存储
			let pageKey = config.KEYS.PAGE_PARAM;
			let pageKeyInfo = config.KEYS.PAGE_PARAM_TRANSFORM_INFO;
			mui.neioConfig.setStorage(pageKey,"")
			mui.neioConfig.setStorage(pageKeyInfo,"")
					
			this.configFormater = config;
			//获取变压器码表 数据字典编码
			let transformerType = this.configFormater.TRANSFORMER_TYPE;
			let dictCodes = transformerType;
			//获取码表
			let dict = mui.neiosCommon.queryDict(dictCodes,function(data){
				if (data != '') {
					//获取变压器字典表Key
					let config = mui.neioConfig.getNeioConfig();
					let key = that.configFormater.KEYS.TRANSFORMER_CODE_LIST;
					//存入变压器类型数据
					mui.neioConfig.setStorage(key,JSON.stringify(data.TRANS_TYPE))
				}
			})
					
		},
		//根据workId获取数据 
		getWorListByType(mParam,current){
			let url = mParam.url;
			let _this = this
			that.workTypeTxt = mParam.text;
			let param = {
				current: current,
				pageSize: 10,
				pageIndex:current,
				workType:mParam.value,
				areaCompanyCode:this.currentUser.companyNewCode,
				// stationCode:localStorage.getItem('neiosStationCode'),
				stationCode:this.currentUser.stationNewCode,
				role:'NEIOS_STATION',
				searchStr:_this.terminalKeyword,
				searchType:'0'
			}
			// 网络设备安全的加 currentUser
			if(param.workType == '0115'){
				Object.assign(param,{
					currentUser:this.currentUser
				})
			}

			//  00:安全活动 01:培训记录 10:应急演练 11:反事故演习 12:事故预想
			let neiosTypeList = ['00','01','10','11','12'];
			// if( neiosTypeList.includes(param.workType)){
			// 	Object.assign(param,{
			// 		searchType:'0',
			// 	})
			// }

			console.log(">>>>>|>:"+JSON.stringify(param)+"地址："+mParam.url)
			mui.neiosRequst.httpRequst(url, param,
				function(data) {
					console.log("获取涉网设备"+JSON.stringify(data))
					if (data.code == '0') {
						_this.isShowList = true
						// console.log(JSON.stringify(data.data.records[0]))
						if (data.data && data.data.records && data.data.records.length > 0 ){
							if( neiosTypeList.includes(param.workType)){
								data.data.records.map((item)=>{
									item['dirllTypeNum'] = param.workType;
								})
							}
							
						}
						if (_this.currentPage == 1) {
							mui('#terminalworkList').pullRefresh().enablePullupToRefresh();
							mui('#terminalworkList').pullRefresh().endPullupToRefresh();
							_this.terminalworkList = data.data.records
						} else {
							mui('#terminalworkList').pullRefresh().endPullupToRefresh();
							_this.terminalworkList = _this.terminalworkList.concat(data.data.records)
						}
						if (data.data.current >= data.data.pages) {
							console.log("====================禁用====================")  
							mui('#terminalworkList').pullRefresh().disablePullupToRefresh();
							mui('#terminalworkList').pullRefresh().endPullupToRefresh(true);
						}
					} else {
						_this.terminalworkList = []
						mui.toast('查询列表失败，请稍后重试！')
					}
				},function(err){
					console.log("获取涉网设备失败"+JSON.stringify(err))
				})
		}
	}
});

mui.plusReady(function() {
	window.addEventListener('isRefresh', function() {
		window.location.reload()
	})
	let pageId = plus.webview;
	window.addEventListener('searchWorkId', function(event) {//A页面自定义事件
		let config = mui.neioConfig.getNeioConfig();
		let key = config.KEYS.WORKTYPE;
		let data = mui.neioConfig.getStorage(key);
		let param = JSON.parse(data);
		paramWork = param;
		console.log(JSON.stringify(param))
		
		terminalwork.searchType = param.value;
		terminalwork.currentPage = 1;
		terminalwork.getWorListByType(param,terminalwork.currentPage)
		
	});
})

mui.init({
	
	pullRefresh: {
		container: "#terminalworkList", //待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
		down: { //下拉刷新
			style: 'circle',
			height: 50, //可选,默认50.触发下拉刷新拖动距离,
			auto: false, //可选,默认false.首次加载自动下拉刷新一次
			contentdown: "下拉可以刷新", //可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
			contentover: "释放立即刷新", //可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
			contentrefresh: "正在刷新...", //可选，正在刷新状态时，下拉刷新控件上显示的标题内容
			callback: async function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
				terminalwork.currentPage = 1;
				terminalwork.getWorListByType(paramWork,terminalwork.currentPage);
				////没有更多内容了，endPulldown 传入true， 不再执行下拉刷新
				mui('#terminalworkList').pullRefresh().endPulldownToRefresh();
				mui('#terminalworkList').pullRefresh().enablePullupToRefresh();
				mui('#terminalworkList').pullRefresh().endPullupToRefresh(true);
			}
		},
		up: { //上拉加载
			height: 50, //可选.默认50.触发上拉加载拖动距离
			auto: false, //可选,默认false.自动上拉加载一次
			contentrefresh: "正在加载...", //可选，正在加载状态时，上拉加载控件上显示的标题内容
			contentnomore: '没有更多数据了', //可选，请求完毕若没有更多数据时显示的提醒内容；
			callback: async function() { //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
				terminalwork.currentPage = terminalwork.currentPage * 1 + 1;
				terminalwork.getWorListByType(paramWork,terminalwork.currentPage);
			}
		}
	},

});

mui('.mui-input-clear')[0].addEventListener('focus', function() {
	mui(".mui-icon-clear")[0].addEventListener('tap', function() {
		terminalwork.terminalKeyword = ''
		// terminalwork.getWorListByType(1);
	})
})
